<template>
  <div class="base-container">
    <t-form  ref="form" :data="queryParams" label-width="80" @submit="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}" @reset="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="申报年度" name="year">
            <t-date-picker style="width: 240px" v-model="queryParams.year" mode="year" clearable/>
          </t-form-item>
          <t-form-item label="评审业务类型" name="reviewType">
            <t-select style="width: 240px" placeholder="评审业务类型" clearable  :options="dict.type.review_type"
                      v-model="queryParams.reviewType">
            </t-select>
          </t-form-item>
          <t-form-item label="是否已进行判定" name="isReviewed">
            <t-select style="width: 240px" v-model="queryParams.isReviewed">
              <t-option value="1"> 是</t-option>
              <t-option value="0"> 否</t-option>
            </t-select>
          </t-form-item>
          <t-form-item label="项目名称" name="projectName">
            <t-input
              v-model="queryParams.projectName"
              clearable
              placeholder="请输入项目名称"
              style="width: 240px"
            />
          </t-form-item>
<!--          <t-form-item label="项目编号" name="projectNum">-->
<!--            <t-input-->
<!--              v-model="queryParams.projectNum"-->
<!--              clearable-->
<!--              placeholder="输入'空'查询未分配"-->
<!--              style="width: 240px"-->
<!--            />-->
<!--          </t-form-item>-->
<!--&lt;!&ndash;          <t-form-item label="申报年度" name="declareYear">&ndash;&gt;-->
<!--&lt;!&ndash;            <t-date-picker  style="width: 240px" v-model="queryParams.declareYear" mode="year" clearable/>&ndash;&gt;-->
<!--&lt;!&ndash;          </t-form-item>&ndash;&gt;-->
<!--          <t-form-item label="承担单位" name="declareDeptName">-->
<!--            <t-input-->
<!--              v-model="queryParams.declareDeptName"-->
<!--              clearable-->
<!--              placeholder="请输入承担单位"-->
<!--              style="width: 240px"-->
<!--            />-->
<!--          </t-form-item>-->
<!--          <t-form-item v-if="site_tag === 'jtt'" label="计划类别" name="projectCategory">-->
<!--            <t-tree-select filterable expand-all clearable v-model="queryParams.projectCategory"  style="width: 240px" :treeProps="{keys: {value: 'id',label:'name'}}"-->
<!--                           :data="projectCategoryOptions" ></t-tree-select>-->
<!--          </t-form-item>-->
<!--          <t-form-item label="学科分类" name="subjectClassification">-->
<!--            <t-select  style="width: 240px" clearable v-model="queryParams.subjectClassification"-->
<!--                       :options="dict.type.subject_classification">-->
<!--            </t-select>-->
<!--          </t-form-item>-->
<!--          <t-form-item label="项目类型" name="projectType">-->
<!--            <t-select  style="width: 240px" clearable v-model="queryParams.projectType"-->
<!--                       :options="dict.type.project_type">-->
<!--            </t-select>-->
<!--          </t-form-item>-->
<!--          <t-form-item label="项目状态" name="projectStatuss">-->
<!--            <t-select :minCollapsedNum="1" style="width: 240px" multiple clearable v-model="queryParams.projectStatuss"-->
<!--                       :options="ProjectStatusOptions">-->
<!--            </t-select>-->
<!--          </t-form-item>-->
<!--          <t-form-item label="建议经费总概算" >-->
<!--            <t-form-item name="startAdviceFunding">-->
<!--            <t-input-->
<!--              v-model="queryParams.startAdviceFunding"-->
<!--              clearable-->
<!--              placeholder="大于等于"-->
<!--              style="width: 120px"-->
<!--            /> </t-form-item>-<t-form-item name="endAdviceFunding"> <t-input-->
<!--            v-model="queryParams.endAdviceFunding"-->
<!--            clearable-->
<!--            placeholder="小于等于"-->
<!--            style="width: 120px"-->
<!--          />-->
<!--            </t-form-item>-->
<!--          </t-form-item>-->
<!--          <t-form-item :label="site_tag==='jtt'?'厅拨经费':'集团补助经费'" >-->
<!--            <t-form-item name="startSubsidyFunding">-->
<!--              <t-input-->
<!--                v-model="queryParams.startSubsidyFunding"-->
<!--                clearable-->
<!--                placeholder="大于等于"-->
<!--                style="width: 120px"-->
<!--              />-->
<!--            </t-form-item> - <t-form-item name="endSubsidyFunding">-->
<!--            <t-input-->
<!--              v-model="queryParams.endSubsidyFunding"-->
<!--              clearable-->
<!--              placeholder="小于等于"-->
<!--              style="width: 120px"-->
<!--            />-->
<!--          </t-form-item>-->
<!--          </t-form-item>-->
<!--          <t-form-item label="项目负责人" name="projectLeaderName">-->
<!--            <t-input-->
<!--              v-model="queryParams.projectLeaderName"-->
<!--              clearable-->
<!--              style="width: 240px"-->
<!--            />-->
<!--          </t-form-item>-->
<!--          <t-form-item label="项目成员" name="projectMemberName">-->
<!--            <t-input-->
<!--              v-model="queryParams.projectMemberName"-->
<!--              clearable-->
<!--              style="width: 240px"-->
<!--            />-->
<!--          </t-form-item>-->

<!--          <t-form-item label="申报日期" name="declareDateRange">-->
<!--            <t-date-range-picker style="width: 280px" :placeholder="['开始时间','结束时间']" v-model="queryParams.declareDateRange"  allow-input clearable/>-->
<!--          </t-form-item>-->
<!--          <t-form-item label="项目时间" name="projectDateRange">-->
<!--            <t-date-range-picker style="width: 280px" v-model="queryParams.projectDateRange" :placeholder="['开始时间','结束时间']" allow-input clearable/>-->
<!--          </t-form-item>-->
<!--          <t-form-item label="管理单位" name="multilevelDeptId">-->
<!--            <template slot="label">-->
<!--              管理单位-->
<!--              <t-tooltip content="查询当前单位及下级单位项目">-->
<!--                <t-icon name="help-circle-filled"></t-icon>-->
<!--              </t-tooltip>-->
<!--            </template>-->
<!--          <t-tree-select  :treeProps="{keys: {value: 'id'}}"  style="width:390px" clearable filterable-->
<!--                           v-model="queryParams.multilevelDeptId" :data="deptMenuOptions"-->
<!--                          placeholder="选择单位">-->
<!--          </t-tree-select>-->
<!--          </t-form-item>-->
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <div class="btn-group-inline">
      <t-button @click="exportExcel" variant="outline" theme="primary">
        <t-icon name="download" slot="icon"/>
        导出
      </t-button>
      <t-button @click="exportReviewExcel" variant="outline" theme="primary">
        <t-icon name="download" slot="icon"/>
        导出评审信息
      </t-button>
    </div>
    <!-- <t-tabs v-if="site_tag === 'jtt'" @change="(val)=>{queryParams.projectCategory=val;getList();}" v-model="queryParams.projectCategory" style="margin-bottom: 10px">
      <t-tab-panel :key="item.id" :value="item.id" v-for="(item) in [{'name': '全部',id: null},...projectCategoryOptions]" :label="item.name" :destroyOnHide="false">
      </t-tab-panel>
    </t-tabs> -->
    <t-tabs @change="(val)=>{queryParams.reviewType=val;getList1();}" v-model="queryParams.projectCategory" style="margin-bottom: 10px">
      <t-tab-panel :key="item.id" :value="item.id" v-for="(item) in queryTypeOptions" :label="item.name" :destroyOnHide="false">
      </t-tab-panel>
    </t-tabs>
    <t-table
      rowKey="index"
      size="small"
      :data="data"
      :columns="queryParams.reviewType === '1'?columns:columns.filter(val=>{ return  val.title!=='评审平均分' })"
      hover
      :pagination="pagination"
      :loading="dataLoading"
      :sort="sort"
      :hideSortTips="true"
       @sort-change="sortChange"
    >
      <template #reviewTeamName="{ row }">
        <t-link @click="dialog.objId=row.teamId;dialog.header='评审组详情';dialog.open_review_group=true" hover="color" theme="primary">{{ row.reviewTeamName }}</t-link>
      </template>
      <template #[`project.projectNum`]="{ row }">
        <div v-if="row?.project?.projectNum">
          {{row?.project?.projectNum}}
        </div>
        <div v-else>
          暂未分配
        </div>
      </template>
      <template #[`project.projectName`]="{ row }">
        <t-link v-if="row.project.version === '2024'" @click="dialog.objId=row.project.id;dialog.header=row.project.projectName;dialog.open_item_detail=true" hover="color" theme="primary">{{row.project.projectName}}</t-link>
        <t-link v-else @click="dialog.objId=row.project.id;dialog.header=row.project.projectName;dialog.open_project_detail=true" hover="color" theme="primary">{{row.project.projectName}}</t-link>
      </template>
      <template #[`project.subjectClassification`]="{ row }">
        {{ row.project.subjectClassification | filterByDict(dict.type.subject_classification) }}
      </template>
      <template #[`project.projectType`]="{ row }">
        {{ row.project.projectType | filterByDict(dict.type.project_type) }}
      </template>
      <template #[`project.adviceFunding`]="{row}">
        {{row.project.adviceFunding}}万元
      </template>
      <template #[`project.subsidyFunding`]="{row}">
        {{row.project.subsidyFunding}}万元
      </template>
      <template #isReviewed="{row}">
        <t-tag theme="success" v-if="row.isReviewed==='1'">
          是
        </t-tag>
        <t-tag theme="danger" v-else-if="row.isReviewed==='0'">
          否
        </t-tag>
        <t-tag theme="warning" v-else>
          未提交
        </t-tag>
      </template>

      <template #op="{ row }">
<!--        <t-button  v-if="row.id.toString().includes('202202')" @click="allApply(row)" size="small" variant="text"-->
<!--                   class="t-button-link">-->
<!--          <t-icon name="edit-1" slot="icon"/>-->
<!--          项目数据完善-->
<!--        </t-button>-->
        <t-button v-if="row.project.version === '2024'" size="small"
                  @click="dialog.objId=row.project.id;dialog.header=row.project.projectName;dialog.open_item_detail=true"
                  variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon"/>
          详情
        </t-button>

        <t-button v-else size="small"
                  @click="dialog.objId=row.project.id;dialog.header=row.project.projectName;dialog.open_project_detail=true"
                  variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon"/>
          详情
        </t-button>
<!--        <t-button @click="createPDF(row)" size="small"-->
<!--                  variant="text" class="t-button-link">-->
<!--          <t-icon name="file-pdf" slot="icon"/>-->
<!--          导出PDF-->
<!--        </t-button>-->
<!--        <t-button @click="dialog.header='审批记录';dialog.objId=row.id;dialog.open_approve_list=true" size="small" variant="text" class="t-button-link">-->
<!--          <t-icon name="info-circle" slot="icon"/>-->
<!--          审批记录-->
<!--        </t-button>-->
        <t-button
                  @click="openReviewDetail(row)"
                  size="small" variant="text"
                  class="t-button-link">
          <t-icon name="root-list" slot="icon"/>
          专家评审详情
        </t-button>
        <t-button
          @click="dialog.objId=row.id;dialog.projectName=row.project.projectName;dialog.projectId=row.project.id;dialog.open_project_approval=true;"
          size="small" variant="text"
          class="t-button-link">
          <t-icon name="check-circle" slot="icon"/>
          评审判定
        </t-button>
      </template>
      <template #dateRange="{ row }">
        {{ row.project.startDate }}-{{ row.project.endDate }}
      </template>
      <template #[`project.projectStatus`]="{ row }">
        <t-tag  @click="dialog.header=row.project.projectName;dialog.objId=row.project.id;dialog.open_approve_list=true" class="cursor-pointer" :theme="handleTheme(row.project.projectStatus, dict.type.project_status)" variant="light">
          {{ row.project.projectStatus | filterByDict(dict.type.project_status) }}
        </t-tag>
      </template>
    </t-table>
    <project-detail-dialog tabDefaultValue="1"  :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_project_detail" v-if="dialog.open_project_detail"></project-detail-dialog>
    <item-detail-dialog tabDefaultValue="1"  :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_item_detail" v-if="dialog.open_item_detail"></item-detail-dialog>
    <approve-list :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_approve_list" v-if="dialog.open_approve_list"></approve-list>
    <review-group-all-detail :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_review_group" v-if="dialog.open_review_group"></review-group-all-detail>
    <project-approval @reload="getList" :header="dialog.header" :projectName="dialog.projectName"
                      :reviewType="queryParams.reviewType" :projectId="dialog.projectId" :objId="dialog.objId"
                      :visible.sync="dialog.open_project_approval"
                      v-if="dialog.open_project_approval"></project-approval>
    <expert-review-list :rowId="dialog.rowId" :expert-leader="dialog.expertLeader" :teamId="dialog.teamId" :objId="dialog.objId" :reviewType="queryParams.reviewType"
                        :projectName="dialog.projectName" :visible.sync="dialog.open_review_list"
                        v-if="dialog.open_review_list"></expert-review-list>
  </div>
</template>

<script>

import ApproveList from "../approve/components/approve-list.vue";
import ProjectDetailDialog from "../pm/components/project-detail-dialog.vue";
import ItemDetailDialog from "../pm/components/item-detail-dialog.vue";
import ReviewGroupAllDetail from "./components/review-group-all-detail.vue";
import dayjs from "dayjs";
import expertReviewList from "./components/expert-review-list.vue";
import projectApproval from "./components/project-approval.vue";

export default {
  name: "kjxm-review-project-index",
  dicts:["project_type", "subject_classification", "sys_user_sex", "prof_tit", "degree", "duty", "platform_type", "member_type", "project_status", "review_type"],
  components:{
    ApproveList,
    ProjectDetailDialog,
    ItemDetailDialog,
    ReviewGroupAllDetail,
    expertReviewList,
    projectApproval
  },
  data(){
    return {
      site_tag: import.meta.env.VITE_SITE_TAG,
      deptMenuOptions: [],
      sort: {
        // 按照 status 字段进行排序
        sortBy: 'sumScore',
        // 是否按照降序进行排序
        descending: true,
      },
      dialog:{
        rowId: null,
        open_project_detail: false,
        header: "",
        objId: 0,
        open_approve_list: false,
        open_item_detail: false,
        open_review_group: false,
        open_review_list: false,
        open_project_approval: false,
      },
      dictTypes: {},
      projectCategoryOptions:[],
      queryTypeOptions: [
        {
          id: "1",
          name: '立项评审',
          total: 0,
        },
        {
          id: "2",
          name: '大纲评审',
          total: 0,
        },
        {
          id: "3",
          name: '验收评审',
          total: 0,
        }
      ],
      columns:[
        // {
        //   colKey: "id",
        //   title: "项目编号",
        //   align: "center",
        //   ellipsis: true
        // },
        // {
        //   colKey: "declareDate",
        //   title: "申报年度",
        //   align: "center"
        // },
        {
          colKey: "project.projectNum",
          title: "项目编号",
          align: "center",
          width: 120,
          // sorter: true,
          // ellipsis: true,
        },
        {
          colKey: "project.projectName",
          title: "项目名称",
          align: "center",
          // ellipsis: true,
          width: 220

        },
        {
          colKey: "reviewTeamName",
          title: "评审组",
          align: "center",
        },
        {
          colKey: "sumScore",
          title: "评审平均分",
          align: "center",
          sorter: true,
          width: 140
        },
        {
          colKey: "project.adviceFunding",
          title: "建议经费总概算",
          align: "center",
          width: 160,
          // sorter: true,
        },
        {
          colKey: "project.subsidyFunding",
          title: import.meta.env.VITE_SITE_TAG==='jtt'?"厅拨经费":`集团补助经费`,
          align: "center",
          // sorter: true,
          width: 140,
        },
        {
          colKey: "project.startDate",
          title: "开始时间",
          align: "center",
          // sorter: true,
          width: 120,
        },
        {
          colKey: "project.endDate",
          title: "结束时间",
          align: "center",
          // sorter: true,
          width: 120,
        },
        {
          colKey: "project.projectLeader.name",
          title: "项目负责人",
          align: "center",
          width: 100
        },
        {
          colKey: "project.nickName",
          title: "填报人",
          align: "center",
          width: 100
        },
        {
          colKey: "project.declareDeptName",
          title: "承担单位",
          align: "center",
          width: 180,
          // sorter: true
        },
        // {
        //   colKey: "project.projectCategoryName",
        //   title: "计划类别",
        //   align: "center",
        //   width: 120
        // },
        {
          colKey: "project.subjectClassification",
          title: "学科分类",
          align: "center",
          width: 100
        },
        {
          colKey: "project.projectType",
          title: "项目类型",
          align: "center",
          width: 120
        },
        {
          colKey: "project.declareDate",
          title: "申报日期",
          align: "center",
          // ellipsis: true,
          width: 110,
          // sorter: true,
        },
        // {
        //   colKey: "attributeDeptName",
        //   title: "归口管理单位",
        //   align: "center"
        // },
        {
          colKey: "project.projectStatus",
          title: "项目状态",
          align: "center",
          width:220,
          fixed: 'right',
          // sorter: true
        },
        {
          colKey: "isReviewed",
          title: "是否已进行判定",
          align: "center",
          width:140,
          fixed: 'right',
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 120,
          fixed: 'right'
        },
      ],
      data:[],
      dataLoading: false,
      queryParams: {
        reviewType: '1',
        year: dayjs().year().toString(),
        sortType: '1',
        isReviewed: '',
        declareYear: '',
        projectDateRange: [],
        declareDateRange: [],
        declareDate: '',
        startDate: '',
        endDate: '',
        startDeclareDate: '',
        endDeclareDate: '',
        startAdviceFunding: null,
        endAdviceFunding: null,
        startSubsidyFunding: '',
        endSubsidyFunding: '',
        multilevelDeptId: null,
        projectLeaderName: '',
        projectMemberName: '',
        pageNum: 1,
        pageSize: 10,
        projectCategory: null,
        projectName: '',
        projectType: '',
        projectNum: '',
        projectStatus: '',
        subjectClassification: '',
        sortGroup: '',
        deptName: '',
        declareDeptName: '',
        projectStatuss: []
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      }
    }
  },
  computed: {
    ProjectStatusOptions() {
      let n
      if (this.dict.type.project_status) {
        n = [{label: '全选', checkAll: true}].concat(this.dict.type.project_status)
      }
      return n
    }
  },
  created() {
    Object.assign(this.queryParams,this.handelQuery(this.queryParams,this.$route.query))
    this.getDeclareTree();
    this.getDeptTreeSelect()
    this.getList();
  },
  methods:{
    openReviewDetail(row) {
      this.dialog.objId = row.project.id;
      this.dialog.teamId = row.teamId;
      this.dialog.projectName = row.project.projectName;
      // 先判断有没有设置专家组长
      // let expertLeader = null;
      // const obj = this.expertList.find((val) => val.teamLeader === '1')
      // if (obj) {
      //   expertLeader = {
      //     id: obj.experts.userId,
      //     nickName: obj.experts.sysUser.nickName
      //   }
      // }
      // this.dialog.expertLeader = expertLeader;
      this.dialog.rowId = row.id;
      this.dialog.open_review_list = true;

    },
    allApply(row){
      this.$router.push({path:`/kjxm/other/pm/all-apply/${row.id}`,query:{title:row.projectName}});
    },
    getDeptTreeSelect() {
      return this.$api.system.dept.treeSelectAllIn().then(response => {
        this.deptMenuOptions = response.data;
        // this.menuOptions = this.handleTree(response.data, "id");
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    exportExcel(){
      this.$download(`/kjxm/excel/reviewTeamProjectInfo`,this.queryParams, `项目列表_${new Date().getTime()}.xls`)
    },
    exportReviewExcel(){
      this.$download(`/kjxm/excel/reviewTeamAllInfo`,this.queryParams, `评审组信息_${new Date().getTime()}.xls`)
    },
    createPDF(row){
      const instance = this.$loading({
        fullscreen: true,
        attach: 'body',
        preventScrollThrough: false,
      });
      this.$api.kjxm.project.projectTestPDF(row.id).then(response => {
        window.open(response.data.fullFilePath, '_blank')
        instance.hide();
      }).catch((e) => {
        instance.hide();
        this.$message.error(e.toString());
      });
    },
    sortChange(sort){
      this.sort = sort;
      let orderByColumn = []
      let isAsc = []
      if(sort instanceof Array){
        orderByColumn = sort.map(val=>val.sortBy)
        isAsc = sort.map(val=>val.descending?`desc`:`asc`)
        sort.forEach(val=>{
          if(val.sortBy === "sumScore"){
            if(val.descending){
              this.queryParams.sortType = '2'
            }else {
              this.queryParams.sortType = '1'
            }
          }
        })
      }else {
        orderByColumn = [sort.sortBy]
        isAsc = [sort.descending?`desc`:`asc`]
        if(orderByColumn.includes('sumScore')){
          if(sort.descending){
            this.queryParams.sortType = '2'
          }else {
            this.queryParams.sortType = '1'
          }
        }
      }
      this.queryParams.orderByColumn = orderByColumn.join(",")
      this.queryParams.isAsc = isAsc.join(",")
      this.getList();
    },
    getList1(){
      this.dataLoading = true
      return this.$api.kjxm.review.reviewListByReviewType(this.queryParams).then(response=>{
        this.pagination.total = response.total
        this.data = response.rows
        this.dataLoading = false
      }).catch(e=>{
        this.dataLoading = false
        this.$message.error(e.toString())
      })
    },
    getList(){
      this.dataLoading = true;
      [this.queryParams.startDate,this.queryParams.endDate] = this.queryParams.projectDateRange;
      [this.queryParams.startDeclareDate,this.queryParams.endDeclareDate] = this.queryParams.declareDateRange;
      this.$api.kjxm.review.projectList(this.queryParams).then(response=>{
        this.pagination.total = response.total;
        this.data = response.rows;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    getDeclareTree() {
      this.$api.kjxm.project.planCategoryList().then(response => {
        this.projectCategoryOptions = this.handleTree(response.data, "id");
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
  }
}
</script>

<style scoped>

</style>
